
#include "subgamedata.h"


        
        
SubGame::SubGame(uint8 machid, String& pid, int32 creditValue, bool8 forMoney)
{
    m_machid = machid;
    m_pid = pid;
    m_creditValue = creditValue;
    m_forMoney = forMoney;
}

        
SubGame::~SubGame()
{
    while ( m_array.size() > 0 )
    {
        SubGameData *sgData = m_array.back();
        delete sgData;
        m_array.pop_back();
    }
}
        
uint8 
SubGame::getMachid()
{
    return m_machid;
}
        
String& 
SubGame::getPid()
{
    return m_pid;
}
        
int32 
SubGame::getCreditValue()
{
    return m_creditValue;
}
        
bool8 
SubGame::isForMoney()
{
    return m_forMoney;
}




void 
SubGame::add(uint8 id, int32 data, bool8 serverOnly)
{
    SubGameData *sgData = new SubGameData();

    sgData->data = data;
    sgData->id = id;
    sgData->serverOnly = serverOnly;

    m_array.push_back(sgData);
}

void 
SubGame::update(uint8 id, int64 data)
{
    SubGameData *sgData;
    bool8 found = false;

    for ( size_t i = 0; i < m_array.size(); i++ )
    {
        sgData = m_array[i];

        if ( sgData->id == id )
        {
            sgData->data = data;
            found = true;
            break;
        }
    }

    if ( !found )
    {
        add(id, data, false);
    }
}

void 
SubGame::setServerOnly(uint8 id, bool8 serverOnly)
{
    SubGameData *sgData;
    bool8 found = false;

    for ( size_t i = 0; i < m_array.size(); i++ )
    {
        sgData = m_array[i];

        if ( sgData->id == id )
        {
			sgData->serverOnly = serverOnly;
            found = true;
            break;
        }
    }

    if ( !found )
    {
        add(id, 0, serverOnly);
    }
}

int64 
SubGame::getData(uint8 id)
{
    SubGameData *sgData;
    int64 data = 0; // Default

    for ( size_t i = 0; i < m_array.size(); i++ )
    {
        sgData = m_array[i];

        if ( sgData->id == id )
        {
            data = sgData->data;
            break;
        }
    }

    return data;
}
        
bool8 
SubGame::isServerOnly(uint8 id)
{
    SubGameData *sgData;
    bool8 serverOnly = true; // Assume restricted access

    for ( size_t i = 0; i < m_array.size(); i++ )
    {
        sgData = m_array[i];

        if ( sgData->id == id )
        {
            serverOnly = sgData->serverOnly;
            break;
        }
    }

    return serverOnly;
}


int32 
SubGame::getDataCount()
{
    return m_array.size();
}

        
int64 
SubGame::getDataAt(int32 index)
{
    int64 data = 0; // Default

    if ( index < (int32)m_array.size() )
    {
        SubGameData *sgData = m_array[index];
        data = sgData->data;
    }
    else
    {
        /**
         * Out of bounds index.
         */
        ASSERT(1);
    }

    return data;
}
        
int32 
SubGame::getIdAt(int32 index)
{
    int id = -1; // implies no data
    if ( index < (int32)m_array.size() )
    {
        SubGameData *sgData = m_array[index];
        id = sgData->id;
    }
    else
    {
        /**
         * Out of bounds index.
         */
        ASSERT(1);
    }

    return id;
}
        
bool8 
SubGame::isServerOnlyAt(int32 index)
{
    bool8 serverOnly = true; // assume restriction
    if ( index < (int32)m_array.size() )
    {
        SubGameData *sgData = m_array[index];
        serverOnly = sgData->serverOnly;
    }
    else
    {
        /**
         * Out of bounds index.
         */
        ASSERT(1);
    }
    return serverOnly;
}

uint8
SubGame::getClientOnlyDataSize()
{
    uint8 datasize = 0;
    uint8 arraysize = m_array.size();
    uint8 serveronlycount = 0;

    for ( int i = 0; i < arraysize; i++ )
    {
        if ( m_array[i]->serverOnly ) 
        {
            ++serveronlycount;
        }
    }

    datasize = arraysize - serveronlycount;

    return datasize;

}

